package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import mest.VO.cxj.ProductOrderVO;
import mest.VO.cxj.ProductJobVO;
import mest.entity.DictionariesOrder;
import mest.entity.bom;
import mest.entity.product_order;
import mest.entity.save_file;
import mest.service.cxj.DictionariesOrderService;
import mest.service.cxj.ProductJobService;
import mest.service.cxj.ProductOrderService;
import mest.service.system.DBService;
import mest.util.NumberConst;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("productOrder")
@CrossOrigin
public class ProductOrderController {

    @Autowired
    private ProductOrderService productOrderService;

    @Autowired
    private ProductJobService productJobService;

    @Autowired
    private DictionariesOrderService dictionariesOrderService;

    @Autowired
    private DBService dbService;


    /**
     * 提供生产单流水号
     *
     * @return
     */
    @GetMapping("getCode")
    public String getCode() {
        String theirDb=dbService.getCompanyDB();
        DictionariesOrder dictionariesOrder = dictionariesOrderService.selectDictionariesOrder(theirDb);
        int productordernumber = dictionariesOrder.getProductOrderNumber();
        return NumberConst.ProductOrder_No + "-" + productordernumber;
    }

    /**
     * 查询全部生产单
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("selectAll")
    public String selectAll(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam(required = false) Integer checkStatus,
                            @RequestParam(required = false) String cfName, @RequestParam(required = false) String planDo,
                            @RequestParam(required = false) String planFinish) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<ProductOrderVO> productOrderList = productOrderService.selectAllProductOrder(page, limit, checkStatus, cfName, planDo, planFinish, theirDb);
            Integer count = productOrderService.selectCountProductOrder(checkStatus, cfName, planDo, planFinish, theirDb);
            resultObj = new ResultObj(0, "success", count, productOrderList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * addProduct查询产品信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "/selectProductInfo")
    @ResponseBody
    public String selectProductInfo(@RequestParam Integer page, @RequestParam Integer limit,
                                    @RequestParam(required = false) String commName) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<ProductJobVO> saveProviderProductJobVOList = productOrderService.selectProductInfo(page, limit, commName,theirDb);
            Integer count = productOrderService.selectProductInfoCount(commName,theirDb);
            resultObj = new ResultObj(0, "success", count, saveProviderProductJobVOList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 保存生产单
     *
     * @param productOrder
     * @param saveList
     * @return
     */
    @PostMapping(value = "/addOrder")
    @ResponseBody
    public String addOrder(@RequestParam String productOrder, @RequestParam String saveList) {
        product_order product_order = JSON.parseObject(productOrder, product_order.class);
        List<ProductJobVO> spJobVO = JSONArray.parseArray(saveList, ProductJobVO.class);
        String theirDb=dbService.getCompanyDB(); //数据库名称
        Integer userId=dbService.getUserId(); //操作人ID
        ResultObj resultObj = null;
        try {
            product_order.setOrderPlacerId(userId);  //默认设置下单人为1，暂时没有session
            int num = productOrderService.insertProductOrder(product_order,theirDb);
            int poId = product_order.getId(); //当前生产单ID
            if (num > 0) {
                dictionariesOrderService.updateDictionariesOrderByProductOrderNumber(theirDb); //单号加1
                int size = productJobService.insertProductJob(productJobService.setSpJobVO(poId, spJobVO),theirDb);
                if (size == spJobVO.size()) {
                    resultObj = ResultObj.builder().code(0).msg("添加成功").data(poId).build(); //操作成功返回生产单ID
                }
            } else {
                resultObj = ResultObj.builder().code(-1).msg("添加失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    @PutMapping(value = "/updateOrder")
    @ResponseBody
    public String updateOrder(@RequestParam String productOrder, @RequestParam String saveList) {
        product_order product_order = JSON.parseObject(productOrder, product_order.class);
        List<ProductJobVO> spJobVO = JSONArray.parseArray(saveList, ProductJobVO.class);
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            //验证ID是否存在,若通过，执行修改操作
            if (productOrderService.isExitId(product_order) && productJobService.isExitId(spJobVO)) {
                int num = productOrderService.updateProductOrder(product_order,theirDb);
                int size = productJobService.updateProductJob(spJobVO,theirDb);
                System.out.println("==========num==========：" + num);
                System.out.println("==========size=========：" + size);
                if (num > 0 && size > 0) {
                    resultObj = ResultObj.builder().code(0).msg("更新成功").build();
                }
            } else { //否则通知失败
                resultObj = ResultObj.builder().code(-1).msg("更新失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 根据存货Id查看物料
     *
     * @param page
     * @param limit
     * @param bomId
     * @return
     */
    @GetMapping("checkBom")
    public String checkBom(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer bomId) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        try {
            List<save_file> saveFileList = productOrderService.selectBomInfo(page, limit, bomId,theirDb);
            Integer count = productOrderService.selectBomInfoCount(bomId,theirDb);
            resultObj = new ResultObj(0, "success", count, saveFileList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }

    /**
     * 审核操作
     *
     * @return
     */
    @PutMapping("updataStatus")
    public String updataStatus(@RequestParam Integer poId) {
        ResultObj resultObj = null;
        String theirDb=dbService.getCompanyDB();
        if (productOrderService.updateProductOrderByStatus(poId,theirDb) > 0) {
            resultObj = ResultObj.builder().code(0).msg("审核成功").build();
        } else {
            resultObj = ResultObj.builder().code(-1).msg("审核失败").build();
        }
        return JSON.toJSONString(resultObj);
    }


}
